using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using Microsoft.ConnectionEngine.Common.Security;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common.DataSets;

namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.DataAccess
{
    /// <summary>
    /// User data access methods
    /// </summary>
    public class AspnetMembership
    {
        #region Get/search methods

        /// <summary>
        /// Loads the details of a specific user membership based on user ID
        /// </summary>
        /// <param name="userID">The ID of the user membership to retrieve</param>
        /// <returns>Returns a typed data table containing the specific data.</returns>
        [CLSCompliant(false)]
        public static dsAspnetMembership GetByUserID(Guid userID)
        {
            dsAspnetMembership dataSet = new dsAspnetMembership();
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());

            dataSet.Load(database.ExecuteReader("spAspnetMembershipSelectByID", userID), LoadOption.OverwriteChanges, "aspnet_Membership");
            return dataSet;
        }

        /// <summary>
        /// Loads the details of a specific user membership based on user name
        /// </summary>
        /// <param name="userName">The user name of the user membership to retrieve</param>
        /// <returns>Returns a typed data table containing the specific data.</returns>
        [CLSCompliant(false)]
        public static dsAspnetMembership GetByUserName(string userName)
        {
            dsAspnetMembership dataSet = new dsAspnetMembership();
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            dataSet.Load(database.ExecuteReader("spAspnetMembershipSelectByUserName", userName), LoadOption.OverwriteChanges, "aspnet_Membership");
            return dataSet;
        }

        /// <summary>
        /// Loads the details of a specific user membership based on email
        /// </summary>
        /// <param name="email">The email of the user membership to retrieve</param>
        /// <returns>Returns a typed data table containing the specific data.</returns>
        [CLSCompliant(false)]
        public static dsAspnetMembership GetByEmail(string email)
        {
            dsAspnetMembership dataSet = new dsAspnetMembership();
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            dataSet.Load(database.ExecuteReader("spAspnetMembershipSelectByEmail", email), LoadOption.OverwriteChanges, "aspnet_Membership");
            return dataSet;
        }


        /// <summary>
        /// Loads the details of all user memberships
        /// </summary>
        /// <returns>Returns a typed data table containing the specific data.</returns>
        [CLSCompliant(false)]
        public static dsAspnetMembership GetAll()
        {
            dsAspnetMembership dataSet = new dsAspnetMembership();
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            dataSet.Load(database.ExecuteReader("spAspnetMembershipSelectAll"), LoadOption.OverwriteChanges, "aspnet_Membership");
            return dataSet;
        }

        #endregion

        #region Insert and update methods

        /// <summary>
        /// Updates the details of a single membership
        /// </summary>
        /// <param name="aspnetMembershipUser">The membership to update</param>
        /// <returns>The number of rows affected</returns>
        public static int Update(AspnetMembershipUser aspnetMembershipUser)
        {
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            return database.ExecuteNonQuery("spAspnetMembershipUpdate", aspnetMembershipUser.ApplicationID, aspnetMembershipUser.UserID, aspnetMembershipUser.Password, aspnetMembershipUser.PasswordFormat, aspnetMembershipUser.PasswordSalt, aspnetMembershipUser.MobilePIN, aspnetMembershipUser.Email, aspnetMembershipUser.LoweredEmail, aspnetMembershipUser.PasswordQuestion, aspnetMembershipUser.PasswordAnswer, aspnetMembershipUser.IsApproved, aspnetMembershipUser.IsLockedOut, aspnetMembershipUser.CreateDate, aspnetMembershipUser.LastLoginDate, aspnetMembershipUser.LastPasswordChangedDate, aspnetMembershipUser.LastLockoutDate, aspnetMembershipUser.FailedPasswordAttemptCount, aspnetMembershipUser.FailedPasswordAttemptWindowStart, aspnetMembershipUser.FailedPasswordAnswerAttemptCount, aspnetMembershipUser.FailedPasswordAnswerAttemptWindowStart, aspnetMembershipUser.Comment);
        }

        /// <summary>
        /// Inserts a user membership into the database
        /// </summary>
        /// <param name="aspnetMembershipUser">The membership data to insert</param>
        /// <returns>The userID of the newly inserted membership row</returns>
        public static Guid Insert(AspnetMembershipUser aspnetMembershipUser)
        {
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            return new Guid(database.ExecuteScalar("spAspnetMembershipInsert", aspnetMembershipUser.ApplicationID, aspnetMembershipUser.UserID, aspnetMembershipUser.Password, aspnetMembershipUser.PasswordFormat, aspnetMembershipUser.PasswordSalt, aspnetMembershipUser.MobilePIN, aspnetMembershipUser.Email, aspnetMembershipUser.LoweredEmail, aspnetMembershipUser.PasswordQuestion, aspnetMembershipUser.PasswordAnswer, aspnetMembershipUser.IsApproved, aspnetMembershipUser.IsLockedOut, aspnetMembershipUser.CreateDate, aspnetMembershipUser.LastLoginDate, aspnetMembershipUser.LastPasswordChangedDate, aspnetMembershipUser.LastLockoutDate, aspnetMembershipUser.FailedPasswordAttemptCount, aspnetMembershipUser.FailedPasswordAttemptWindowStart, aspnetMembershipUser.FailedPasswordAnswerAttemptCount, aspnetMembershipUser.FailedPasswordAnswerAttemptWindowStart, aspnetMembershipUser.Comment).ToString());
        }

        #endregion

        #region Delete methods

        /// <summary>
        /// Deletes a single user membership
        /// </summary>
        /// <param name="userID">The ID of the user membership</param>
        /// <returns>Returns the number of rows affected</returns>
        public static int Delete(Guid userID)
        {
            Database database = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            return database.ExecuteNonQuery("spAspnetMembershipDelete", userID);
        }

        #endregion
    }
}

